package com.zf.zfed.audit.ui

import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.zf.zfed.audit.R

/**
 * 登录管理Activity
 */
class LoginManagementActivity : AppCompatActivity() {
    
    private lateinit var btnAdmin: RoleButton
    private lateinit var btnConfidentiality: RoleButton
    private lateinit var btnAuditor: RoleButton
    private lateinit var btnOperator: RoleButton
    private lateinit var cardPassword: PasswordManagementCard
    private lateinit var cardFingerprint1: FingerprintManagementCard
    private lateinit var cardFingerprint2: FingerprintManagementCard
    
    private var currentRole: String = "保密员" // 默认选中保密员
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login_management)
        
        initViews()
        setupClickListeners()
        updateUIForCurrentRole()
    }
    
    private fun initViews() {
        btnAdmin = findViewById(R.id.btn_admin)
        btnConfidentiality = findViewById(R.id.btn_confidentiality)
        btnAuditor = findViewById(R.id.btn_auditor)
        btnOperator = findViewById(R.id.btn_operator)
        cardPassword = findViewById(R.id.card_password)
        cardFingerprint1 = findViewById(R.id.card_fingerprint1)
        cardFingerprint2 = findViewById(R.id.card_fingerprint2)
    }
    
    private fun setupClickListeners() {
        // 返回按钮
        findViewById<android.widget.ImageView>(R.id.btn_back).setOnClickListener {
            finish()
        }
        
        // 角色选择按钮
        btnAdmin.setOnClickListener { selectRole("管理员") }
        btnConfidentiality.setOnClickListener { selectRole("保密员") }
        btnAuditor.setOnClickListener { selectRole("审计员") }
        btnOperator.setOnClickListener { selectRole("操作员") }
        
        // 密码管理按钮
        cardPassword.setOnModifyClickListener {
            // TODO: 实现修改密码逻辑
            Toast.makeText(this, "修改密码 - $currentRole", Toast.LENGTH_SHORT).show()
        }
        
        cardPassword.setOnResetClickListener {
            // TODO: 实现重置密码逻辑
            Toast.makeText(this, "重置密码 - $currentRole", Toast.LENGTH_SHORT).show()
        }
        
        cardPassword.setOnUnlockClickListener {
            // TODO: 实现解锁密码逻辑
            Toast.makeText(this, "解锁密码 - $currentRole", Toast.LENGTH_SHORT).show()
        }
        
        // 指纹管理
        cardFingerprint1.setOnActionClickListener {
            // TODO: 实现指纹1操作逻辑
            val action = if (cardFingerprint1.getFingerprintStatus() == "已录指纹") "删除指纹1" else "添加指纹1"
            Toast.makeText(this, "$action - $currentRole", Toast.LENGTH_SHORT).show()
        }
        
        cardFingerprint2.setOnActionClickListener {
            // TODO: 实现指纹2操作逻辑
            val action = if (cardFingerprint2.getFingerprintStatus() == "已录指纹") "删除指纹2" else "添加指纹2"
            Toast.makeText(this, "$action - $currentRole", Toast.LENGTH_SHORT).show()
        }
    }
    
    private fun selectRole(role: String) {
        currentRole = role
        
        // 更新按钮选中状态
        btnAdmin.setSelected(role == "管理员")
        btnConfidentiality.setSelected(role == "保密员")
        btnAuditor.setSelected(role == "审计员")
        btnOperator.setSelected(role == "操作员")
        
        // 更新UI
        updateUIForCurrentRole()
        
        Toast.makeText(this, "已选择角色: $role", Toast.LENGTH_SHORT).show()
    }
    
    private fun updateUIForCurrentRole() {
        // 根据角色更新密码管理按钮状态
        val isPasswordEnabled = currentRole != "管理员"
        cardPassword.setButtonsEnabled(isPasswordEnabled)
        cardPassword.setButtonsVisible(isPasswordEnabled)
        
        // TODO: 根据角色更新指纹状态
        updateFingerprintStatus()
    }
    
    private fun updateFingerprintStatus() {
        // TODO: 从数据库获取当前角色的指纹状态
        when (currentRole) {
            "管理员" -> {
                // 管理员默认没有指纹
                cardFingerprint1.setFingerprintStatus("未录指纹")
                cardFingerprint1.setActionIcon(R.drawable.u3240)
                cardFingerprint2.setFingerprintStatus("未录指纹")
                cardFingerprint2.setActionIcon(R.drawable.u3240)
            }
            "保密员" -> {
                // 保密员默认有指纹1
                cardFingerprint1.setFingerprintStatus("已录指纹")
                cardFingerprint1.setActionIcon(R.drawable.u395)
                cardFingerprint2.setFingerprintStatus("未录指纹")
                cardFingerprint2.setActionIcon(R.drawable.u3240)
            }
            "审计员" -> {
                // 审计员默认没有指纹
                cardFingerprint1.setFingerprintStatus("未录指纹")
                cardFingerprint1.setActionIcon(R.drawable.u3240)
                cardFingerprint2.setFingerprintStatus("未录指纹")
                cardFingerprint2.setActionIcon(R.drawable.u3240)
            }
            "操作员" -> {
                // 操作员默认有指纹1和指纹2
                cardFingerprint1.setFingerprintStatus("已录指纹")
                cardFingerprint1.setActionIcon(R.drawable.u395)
                cardFingerprint2.setFingerprintStatus("已录指纹")
                cardFingerprint2.setActionIcon(R.drawable.u395)
            }
        }
    }
}
